﻿using Dapper;
using Entity.Utils;
using EntityLibrary.Business.ClinicAndPay.Entity;
using EntityLibrary.Business.ClinicAndPay.Sql;
using EntityLibrary.DomainCheck;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;

namespace EntityLibrary.Business.ClinicAndPay
{
    public class ClinicAndPayService
    {
        string connStr = Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["HISDATA"].ConnectionString);

        //查询缴费记录 flag 1 未缴费的记录 2 已缴费的记录 3全部
        public bool GetPaymentSheetList(int CallNo, string CallName, string patientId, out IList<RegistSheet> registSheetList, int flag = 1)
        {
            if (default(KeyValuePair<string, string>).Equals(
             CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
             ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetPaymentSheetList.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@Flag_In", flag);
            sqlParam.Add("@PatientId_In", patientId);

            registSheetList = conn.Query<RegistSheet>(sqlStr, sqlParam).ToList();
            if (registSheetList.Count.Equals(0))
            {
                throw new InvalidOperationException("未查找到相应缴费记录");
            }
            conn.Close();
            return true;
        }

        //查询处方费用清单(未缴费)
        public bool GetUnPayRecipeList(int CallNo, string CallName, string MZNum, string recipeNum, out IList<Recipe> recipeList)
        {
            if (default(KeyValuePair<string, string>).Equals(
             CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
             ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            conn.Open();
            string sqlStr = SQLFileHelper.GetSql("GetUnPayRecipeList.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@MZNum_In", MZNum);
            sqlParam.Add("@RecipNum_In", recipeNum);
            recipeList = conn.Query<Recipe>(sqlStr, sqlParam).ToList();
            if (recipeList.Count.Equals(0))
            {
                throw new InvalidOperationException("未查找到相应的缴费记录");
            }
            conn.Close();
            return true;
        }

        //查询处方费用清单(已缴费)
        public IList<RecipePaid> GetPaidRecipeList(int CallNo, string CallName, string MZNum, string recipeNum)
        {
            if (default(KeyValuePair<string, string>).Equals(
             CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
             ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            conn.Open();
            string sqlStr = SQLFileHelper.GetSql("GetPaidRecipeList.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@MZNum_In", MZNum);
            sqlParam.Add("@RecipNum_In", recipeNum);
            var recipePaidList = conn.Query<RecipePaid>(sqlStr, sqlParam).ToList();
            conn.Close();
            return recipePaidList;
        }

        //门诊缴费确认(按门诊号来,一次缴完)
        public bool PayRecipe(int CallNo, string CallName, string patientId, int MZNum
                              , string orderNumber, int payType
                              , string payNumber, string isMedicarePay, string medicareNumber
                              , decimal payAmount, decimal medicareAmount, decimal totalAmount
                              , string extend, string terminalId
                              , ref string errorMessage, out List<ClinicPayResult> recipeList)
        {
            if (default(KeyValuePair<string, string>).Equals(CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            var conn = new SqlConnection(connStr);
            conn.Open();
            var trans = conn.BeginTransaction();
            string sqlStr = SQLFileHelper.GetSql("PayRecipe.sql");
            var sqlParam = new DynamicParameters();

            sqlParam.Add("@CallNo_In", CallNo);
            sqlParam.Add("@CallName_In", CallName);
            sqlParam.Add("@patientId_In", patientId);
            sqlParam.Add("@MZNum_In", MZNum);
            sqlParam.Add("@PayWay_In", payType);
            //6（诊间支付这个设计卡内预存金额）9第三方支付
            sqlParam.Add("@YBFlag_In", 9);
            sqlParam.Add("@PayMoney_In", payAmount);
            sqlParam.Add("@TransNo_In", orderNumber);
            sqlParam.Add("@BanktransNo_In", payNumber);
            sqlParam.Add("@PayDeviceId_In", terminalId);
            try
            {
                recipeList = conn.Query<ClinicPayResult>(sqlStr, sqlParam, trans).ToList();
                foreach (ClinicPayResult result in recipeList)
                {
                    if (result.Result.Equals("-1"))
                        throw new InvalidOperationException(result.ResultText);
                }

                trans.Commit();
                return true;
            }
            catch (Exception e)
            {
                recipeList = null;
                trans.Rollback();
                errorMessage = e.Message;
                return false;
            }
            finally
            {
                trans.Dispose();
                conn.Close();
            }
        }

        //查询挂号费用记录
        public List<RegistPaymentRecords> GetRegistPayRecord(int CallNo, string CallName, string patientId)
        {
            if (default(KeyValuePair<string, string>).Equals(
            CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetRegistPayRecord.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@PatientId_In", patientId);

            var registRcordList = conn.Query<RegistPaymentRecords>(sqlStr, sqlParam).ToList();
            conn.Close();
            return registRcordList;
        }

    }
}
